<template>
  <div>
    <el-dialog
      :title="title"
      :visible.sync="dialogVisible"
      width="36%"
      :close-on-click-modal="false"
      :close-on-press-escape="false"
      @closed="resetForm"
      @close="resetForm"
    >
      <el-form ref="ruleForm" :model="ruleForm" :rules="rules" label-width="120px" class="demo-ruleForm">
        <el-form-item label="参数名称" prop="type_name">
          <el-input v-model="ruleForm.type_name" placeholder="请输入参数名称" />
        </el-form-item>
        <el-form-item label="参数标识" prop="description">
          <el-input v-model="ruleForm.description" placeholder="请输入参数标识" />
        </el-form-item>
        <el-form-item label="参数类型" prop="location">
          <el-radio v-model="ruleForm.location" :label="0">启用</el-radio>
          <el-radio v-model="ruleForm.location" :label="1">禁用</el-radio>
        </el-form-item>
        <el-form-item label="单位" prop="description">
          <el-input v-model="ruleForm.unit" placeholder="请输入单位" />
        </el-form-item>
        <el-form-item style="display: flex;justify-content: flex-end">
          <el-button type="primary" @click="resetForm">取消</el-button>
          <el-button type="primary" @click="addOrEdit">确定</el-button>
        </el-form-item>
      </el-form>
    </el-dialog>
  </div>
</template>

<script>
import { createDataType, updateDataType, detailDataType } from '@/api/dataType'

export default {
  name: 'detail',
  props: {
    dialogVisible: {
      type: Boolean,
      default: true
    },
    title: {
      type: String,
      default: '新增'
    },
    id: {
      type: Number,
      default: 0
    }
  },
  data() {
    return {
      ruleForm: {
        type_name: '',
        description: '',
        location: 0,
        pid: 0,
        unit: null
      },
      rules: {
        type_name: [
          { required: true, message: '请输入参数名称', trigger: 'blur' }
        ],
        description: [
          { required: true, message: '请输入参数标识', trigger: 'blur' }
        ],
        location: [
          { required: true, message: '请输入参数类型', trigger: 'blur' }
        ]
      }
    }
  },
  created() {
    // 编辑
    if (this.id) {
      // this.ruleForm.pid = this.id
      if (this.title === '新增') {
        this.ruleForm.pid = this.id
      } else {
        this.getDetailById(this.id)
      }
    }
  },
  methods: {
    // 获取详情
    getDetailById(id) {
      detailDataType({ id }).then(res => {
        this.ruleForm = res.data
        this.timeArr = [res.data.company_start, res.data.company_end]
      })
    },
    // 新增、编辑
    addOrEdit() {
      this.$refs['ruleForm'].validate((valid) => {
        if (valid) {
          if (this.title === '编辑') {
            //  是编辑
            updateDataType(this.ruleForm).then(res => {
              this.$message({
                message: '修改成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          } else {
            //  是新增
            createDataType(this.ruleForm).then(res => {
              this.$message({
                message: '新增成功',
                type: 'success'
              })
              this.$emit('confirm')
            })
          }
        }
      })
    },
    // 表单重置
    resetForm() {
      this.$refs['ruleForm'].resetFields()
      this.$emit('close')
    }
  }
}
</script>
<style lang="scss" scoped>
</style>
